package com.ruoyi.common.db;



import com.alibaba.druid.DbType;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.*;
import com.jfinal.plugin.druid.DruidPlugin;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class DbLink {
    public static Boolean linkDb(Record data)
    {
        boolean flage=false;
        try
        {
            String dbname = data.getStr("code") ;
            String url = data.getStr("dataBaseURL");
            String username = data.getStr("dataBaseUserName");
            String password = data.getStr("dataBaseUserPassword");
            int initialSize = Integer.parseInt(data.getStr("initialSize").toString()) ;
            int minIdle = Integer.parseInt(data.getStr("minIdle").toString()) ;
            int maxActive = Integer.parseInt(data.getStr("maxActive").toString()) ;
            String dataBaseType=data.getStr("dataBaseType");
            boolean showSql = "Y".equalsIgnoreCase(data.getStr("showSql"));
            boolean toLowerCase = true;
            DruidPlugin dp=new DruidPlugin(url,username,password);
            dp.set(initialSize, minIdle, maxActive);
            //dp.setDriverClass(driver_class);
            DbType dbType = null;
            Dialect dialect = null;
            if(dataBaseType.equals("ORACLE")){
                dbType = JdbcConstants.ORACLE;
                dialect = new OracleDialect();
                dp.setValidationQuery("select 1 from dual");
            }
            else if(dataBaseType.equals("SQL_SERVER")){
                dbType = JdbcConstants.SQL_SERVER;
                dialect = new AnsiSqlDialect();
            }
            else if(dataBaseType.equals("MYSQL")){
                dbType = JdbcConstants.MYSQL;
                dialect = new MysqlDialect();
            }
            else if(dataBaseType.equals("POSTGRESQL")){
                dbType = JdbcConstants.POSTGRESQL;
                dialect = new PostgreSqlDialect();
            }
            else{
                throw new RuntimeException("不支持的数据库！");
            }
            WallFilter wallFilter = new WallFilter();
            wallFilter.setDbType(dbType);
            dp.addFilter(wallFilter);
            dp.addFilter(new StatFilter());
            ActiveRecordPlugin arp = new ActiveRecordPlugin(dbname,dp);
            arp.setShowSql(showSql);
            arp.setDialect(dialect);
//            arp.setContainerFactory(new CaseInsensitiveContainerFactory(toLowerCase));
            // 不区分大小写 但是最后字段名统一小写
            arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
            if(dp.start())
            {
                if(arp.start())
                {
                    flage=true;
                    log.info("************数据源["+dbname+"]--["+ data.getStr("systemName")+"]启动成功************");
                }
            }

        }catch (Exception e )
        {
            e.printStackTrace();
        }
        return flage;
    }
}
